<?php

/**
 * 后台文章管理
 * @author  xiaobai
 * @date    2017-01-12
 */

namespace app\panel\controller;

use app\common\controller\Panelbase;
use app\common\model\News as news_model;
use app\panel\logic\News as news_logic;
use app\common\model\PositionData;
use app\common\model\TagData;
use think\facade\Request;
use app\common\logic\Search;

class News extends Panelbase
{
    public function initialize()
    {
        parent::initialize();
        $this->column        = model('Common/Column');
        $this->model         = new news_model();
        $this->news_logic    = new news_logic();
        $this->position      = model('common/Position');
        $this->position_data = new PositionData();
        $this->tag_data      = new TagData();
        $this->validate_name = 'news';

        $this->assign('is_tops', news_model::$is_tops);
        $this->assign('is_originals', news_model::$is_original);
    }

    public function index()
    {
        $formParams = input('param.');
        $search_res = $this->news_logic->searchWhere($formParams);

        $where       = $search_res['where'];
        $count_where = $search_res['count_where'];
        $params      = $search_res['params'];
        $search_form = $search_res['formParams'];

        $count = $this->model->where($count_where)->count('id');

        $list  = $this->model->field('a.*, b.name as col_name')
                      ->alias('a')
                      ->join('__COLUMN__ b ','b.id= a.column_id')
                      ->where($where)
                      ->order('a.id desc')->paginate(config('other.page_num'), false, array('query' => $params));

        $page = $list->render();

        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('formParams', $search_form);

        $column_id = !empty($formParams['column_id']) ? $formParams['column_id'] : 0;
        $columns   = $this->column->selectColumn(0, array('type'=>1));

        $this->assign('columns', $columns);

        return $this->fetch();
    }

    public function add()
    {
        $parentid = 0;

        // 栏目树形列表
        $where   = array('type'=>array('in', '1,3'), 'is_news'=>1);
        $columns = $this->column->selectColumn($parentid, $where);

        // 推荐位列表
        $pos_list = $this->position->getList();

        $this->assign('editor_type', config('template.system_editor'));
        $this->assign('pos_list', $pos_list);
        $this->assign('columns', $columns);
        return $this->fetch();
    }

    public function add_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            // 数据验证
            $result = $this->validate($data,'news');
            if (true !== $result) {
                $this->error($result);
            }

            $res = $this->news_logic->saveNews($data);
            if ($res['code'] == 1) {
                $this->error($res['msg']);
            }

            $this->success('保存成功', Url('index'));
        } else {
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id   = input("param.id");
        $news = $this->model->where('id',$id)->find()->toArray();
        if (empty($news)) {
            $this->error('数据不存在');
        }

        // 栏目树形列表
        $where   = array('type'=>array('in', '1,3'), 'is_news'=>1);
        $columns = $this->column->selectColumn(0, $where);
        // 推荐位列表
        $pos_list = $this->position->getList();

        // 文章所选推荐位处理
        $news['position_ids'] = $this->position_data->getNewsPositionIds($id);

        // 获取当前文章的tag标签id
        $tag_data = $this->tag_data->getTagData($id);
        $tag_name = [];
        if (!empty($tag_data)) {
            foreach ($tag_data as $k=>$v) {
                $tag_name[] = $v['name'];
            }
        }
        $news['tags'] = implode(',', $tag_name);

        $this->assign('editor_type', config('template.system_editor'));
        $this->assign('pos_list', $pos_list);
        $this->assign('columns', $columns);
        $this->assign($news);

        return $this->fetch();
    }

    public function edit_post()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 数据验证
            $result = $this->validate($data, $this->validate_name);
            if (true !== $result) {
                $this->error($result);
            }

            $res = $this->news_logic->saveNews($data, $data['id']);
            if ($res['code'] == 1) {
                $this->error($res['msg']);
            }

            $this->success('保存成功', Url('index'));
        } else {
            $this->error('请求错误');
        }
    }

    public function delete()
    {
        $id = input("param.id", 0, 'intval');
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误');
        }
        $control_array = array('Group');
        $control = request::instance()->controller();
        if (in_array($control, $control_array) && $id == 1) {
            $this->error('当前数据不支持删除');
        }
        $res = $this->model->where(array('id'=>$id))->delete();
        if ($res !== false) {
            $xun_search_no = !empty(env('xun_search_no')) ? env('xun_search_no') : 0;
            if ($xun_search_no <= 0) {
                $xun_search = new Search();
                @$xun_search->delNewsIndex($id);
            }
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
